<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Lisensi extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->helper('flexigrid');
        $this->load->library('flexigrid');
        $this->load->model('lisensi_model');
		$this->load->library('form_validation');
		$this->cek_login();
    }

	
	function cek_login(){
		$id_hak = $this->session->userdata('id_hak');
		if($id_hak != 1 || $id_hak == ''){
			redirect ('login');
		}
		
	}

    //load Grid
    function index() {
        $colModel['no'] = array('No', 30, TRUE, 'center', 0);
        $colModel['Nama_Lisensi'] = array('Nama Lisensi', 200, TRUE, 'center', 1);
        $colModel['Alamat'] = array('Alamat', 270, TRUE, 'center', 0);
        $colModel['CP'] = array('Contact Person', 200, TRUE, 'center', 0);
        $colModel['Edit'] = array('Edit', 30, TRUE, 'center', 0);
        $colModel['Lihat_Seluruh_Data_Lisensi'] = array('Lihat Data Lisensi', 130, TRUE, 'center', 0);

        $gridParams = array(
            'width' => 'auto',
            'height' => 300,
            'rp' => 10,
            'rpOptions' => '[5,10,15,20,25,40]',
            'pagestat' => 'Menampilkan: {from} hingga {to} dari {total} data.',
            'blockOpacity' => 0.5,
            'title' => 'Daftar Lisensi',
            'showTableToggleBtn' => true
        );

        //$buttons[] = array('Tambah','add','test');
        //$buttons[] = array('separator');

        $buttons[] = array('Tambah', 'add', 'spt_js');
        $buttons[] = array('separator');
        $buttons[] = array('Hapus', 'delete', 'spt_js');

        // mengambil data dari file controler ajax pada method grid_berkas
        $grid_js = build_grid_js('flex1', site_url("lisensi/lisensi/grid_lisensi"), $colModel, 'Nama_Lisensi', 'asc', $gridParams, $buttons);


        //additional JS
        $data['added_js'] =
                "<script type='text/javascript'>
		function spt_js(com,grid)
		{
			if (com=='Tambah')
			{
				location.href='" . base_url() . "index.php/lisensi/lisensi/form_lisensi'; 
			}
			
			if (com=='Hapus')
				{
				   if($('.trSelected',grid).length>0){
					   if(confirm('Anda yakin menghapus ' + $('.trSelected',grid).length + ' data lisensi?')){
							var items = $('.trSelected',grid);
							var itemlist ='';
							for(i=0;i<items.length;i++){
								itemlist+= items[i].id.substr(3)+',';
							}
							$.ajax({
							   type: 'POST',
							   url: '" . base_url() . "index.php/lisensi/lisensi/delete" . "',
							   data: 'items='+itemlist,
							   success: function(data){
								$('#flex1').flexReload();
							   }
							});
						}
					} else {
						return false;
					} 
				}
				
		} 
		</script>
		";

        $data['js_grid'] = $grid_js;
        //remdering view
        $data['title'] = 'Daftar Batch';
        $this->template->write_view('header', 'template/header', $data);
        $this->template->write_view('content', 'lisensi/grid_lisensi', $data);
        $this->template->write_view('footer', 'template/footer', $data);
        $this->template->render();
    }

    function grid_lisensi() {
        $valid_fields = array('Nama_Lisensi', 'Alamat');
        $this->flexigrid->validate_post('Nama_Lisensi', 'asc', $valid_fields);
        $records = $this->lisensi_model->get_data_lisensi();
        $this->output->set_header($this->config->item('json_header'));

        $no = 0;
        foreach ($records['records']->result() as $row) {
            $link_edit = "<a href='" . base_url() . "index.php/lisensi/lisensi/edit/" . $row->ID_LISENSI . "'>";
            $image_edit = "" . base_url() . "images/grid/edit.png";
            $link_daftar_batch = "<a href='" . base_url() . "index.php/batch/batch/daftar/" . $row->ID_LISENSI . "'>";
            $image_daftar_batch = "" . base_url() . "images/grid/peserta.png";

            $record_items[] = array(
                $row->ID_LISENSI,
                $no = $no + 1,
                $row->NAMA_LISENSI,
                $row->ALAMAT,
                $row->CP,
                '' . $link_edit . '<img border=\'0\' src=\'' . $image_edit . '\'></a> ',
                '' . $link_daftar_batch . '<img border=\'0\' src=\'' . $image_daftar_batch . '\'></a> '
                    //'<a href=\''.base_url().'index.php/peserta/peserta/daftar/'.$row->ID_BATCH.'\'><img border=\'0\' src=\''.base_url().'images/grid/peserta.png\'></a> '
            );
        }

        if (isset($record_items))
            $this->output->set_output($this->flexigrid->json_build($records['record_count'], $record_items));
        else
            $this->output->set_output('{"page":"1","total":"0","rows":[]}');
    }

    public function form_lisensi() {
        $data['status'] = 'new';
        $data['title'] = 'Tambah Lisensi';

        $this->template->write_view('header', 'template/header', $data);
        $this->template->write_view('content', 'lisensi/form_lisensi', $data);
        $this->template->write_view('footer', 'template/footer', $data);
        $this->template->render();
    }

    public function insert_lisensi() {
		if($this->cek_validasi() == FALSE){
			$this->form_lisensi();
		}
		else {
			$data = array(
				'NAMA_LISENSI' => $this->input->post('nama'),
				'ALAMAT' => $this->input->post('alamat'),
				'CP' => $this->input->post('cp'),
				'STATUS' => 1
			);
			$this->lisensi_model->insert_lisensi($data);
			redirect('lisensi/lisensi');
		}
    }

    public function edit($id) {
        $data['status'] = 'edit';
        $record_peserta = $this->lisensi_model->selectone($id);
        foreach ($record_peserta->result() as $peserta) {
            $data['id_lisensi'] = $peserta->ID_LISENSI;
            $data['nama_lisensi'] = $peserta->NAMA_LISENSI;
            $data['alamat_lisensi'] = $peserta->ALAMAT;
            $data['cp'] = $peserta->CP;
        }
        $data['title'] = 'Edit Lisensi';
        $this->template->write_view('header', 'template/header', $data);
        $this->template->write_view('content', 'lisensi/form_lisensi', $data);
        $this->template->write_view('footer', 'template/footer', $data);
        $this->template->render();
    }

    //update peserta
    public function update_lisensi($id) {
        $data = array(
            'NAMA_LISENSI' => $this->input->post('nama'),
            'ALAMAT' => $this->input->post('alamat'),
            'CP' => $this->input->post('cp'),
        );
        $this->lisensi_model->update_lisensi($id, $data);
        redirect('lisensi/lisensi');
    }

    public function delete($id) {
        $spt_ids_post_array = split(",", $this->input->post('items'));
        foreach ($spt_ids_post_array as $index => $id) {
            if (isset($id) && $id != '') {
                $this->lisensi_model->delete($id);
            }//end if
        }//end foreach
    }
	
	
	function cek_validasi() {
        // Setting Rules
        $this->form_validation->set_rules('nama', 'Nama Lisensi', 'required');
        $this->form_validation->set_rules('alamat', 'Alamat', 'required');
        $this->form_validation->set_rules('cp', 'Contact Person', 'required');

        //Setting Error Message
        $this->form_validation->set_message('required', 'Kolom %s harus diisi.');
        
        // Setting Delimiter
        $this->form_validation->set_error_delimiters('<li class="error">', '</li>');
        return $this->form_validation->run();
    }

}

/* End of file pegawai.php */
/* Location: ./application/controllers/pegawai.php */